require.config({
	shim:{
        'calendar':{exports:'calendar'}
    },
    paths : {
        //"vue":"lib/vue",
        "jquery":"lib/jquery",
        "cmd":"cmd",
        "calendar":"lib/datepicker/WdatePicker",
    }
});

require(['jquery','cmd','calendar'],function($){

//get top menu
var token = detectToken();


//获取新增控制器类型列表
var getAllDeviCtrTypes = function(){
    var typeName = $(".enterYouDevices").val();
    $.ajax({ 
        type:"post", 
        url:urls.getDeviceCTRlists, 
        data: {token:token,pageNow:1,pageSize:100,typeName:typeName}, 
        dataType: 'json', 
        async : true, 
        success:function(result){
            var getBaseInfoDevicesCTRHtml = '';
            if(result.data.records.length==0){
                $(".baseInfo-searchShowlist").html('<p style="text-align:center;padding:19px 0;color:#929292;">没有搜索到相关设备</p>');
                $(".baseInfoSaveBtn").fadeOut(500);
            }else{
                $(".baseInfoSaveBtn").fadeIn(500);
            for(var i=0;i<result.data.records.length;i++){
                            getBaseInfoDevicesCTRHtml +='<div data-id="'+result.data.records[i].id+'" class="baseInfosearchlistEach"><i class="baseInfo-nocheck"></i><span>'+result.data.records[i].typeName+'</span></div>';
                        }
                        $(".baseInfo-searchShowlist").html(getBaseInfoDevicesCTRHtml);
                        //========默认选中第一个控制器，并显示第一个控制器参数SATRT========
                        $(".baseInfosearchlistEach").eq(0).find("i").addClass("baseInfo-checked");
                        var deFault_typeID = $(".baseInfosearchlistEach").eq(0).attr("data-id");
                        var checkEveryDevicesParams = function(eachID){
                            $.vloading.show();
                            $.ajax({ 
                                type:"post", 
                                url:urls.checkallDeviceTypeParam, 
                                data: {token:token,device_type_id:eachID}, 
                                dataType: 'json', 
                                async : true, 
                                success:function(result){
                                    $.vloading.remove();
                                    var showCtrParamsBoxShowHtml1 = '<span><input id="ckAll" type="checkbox" /><label>全选</label></span>';
                                    var showCtrParamsBoxShowHtml2 = '';
                                    var showCtrParamsBoxShowHtml3 = '';
                                    var showCtrParamsBoxShowHtml4 = '';
                                    var showCtrParamsBoxShowHtml5 = '';
                                    var showCtrParamsBoxShowHtml6 = '';
                                    var showCtrParamsBoxShowHtml7 = '';
                                    var showCtrParamsBoxShowHtml8 = '';
                                    for(var i=0;i<result.data.records.length;i++){
                                        if(result.data.records[i].param_category==1){//分类1
                                            if(result.data.records[i].device_type_param_id==""){//未选中
                                                showCtrParamsBoxShowHtml1 +='<span><input name="paramszd" type="checkbox" value="'+result.data.records[i].id+'" /><label>'+result.data.records[i].param_name+'</label></span>';
                                            }else{//选中
                                                showCtrParamsBoxShowHtml1 +='<span><input name="paramszd" type="checkbox" value="'+result.data.records[i].id+'" checked="checked" /><label>'+result.data.records[i].param_name+'</label></span>';
                                            }
                                            $(".showCtrParamsBox1").html(showCtrParamsBoxShowHtml1);
                                        }else if(result.data.records[i].param_category==2){//分类2
                                            if(result.data.records[i].device_type_param_id==""){//未选中
                                                showCtrParamsBoxShowHtml2 +='<span><input name="paramszd" type="checkbox" value="'+result.data.records[i].id+'" /><label>'+result.data.records[i].param_name+'</label></span>';
                                            }else{//选中
                                                showCtrParamsBoxShowHtml2 +='<span><input name="paramszd" type="checkbox" value="'+result.data.records[i].id+'" checked="checked" /><label>'+result.data.records[i].param_name+'</label></span>';
                                            }
                                            $(".showCtrParamsBox2").html(showCtrParamsBoxShowHtml2);
                                        }else if(result.data.records[i].param_category==3){//分类3
                                            if(result.data.records[i].device_type_param_id==""){//未选中
                                                showCtrParamsBoxShowHtml3 +='<span><input name="paramszd" type="checkbox" value="'+result.data.records[i].id+'" /><label>'+result.data.records[i].param_name+'</label></span>';
                                            }else{//选中
                                                showCtrParamsBoxShowHtml3 +='<span><input name="paramszd" type="checkbox" value="'+result.data.records[i].id+'" checked="checked" /><label>'+result.data.records[i].param_name+'</label></span>';
                                            }
                                            $(".showCtrParamsBox3").html(showCtrParamsBoxShowHtml3);
                                        }else if(result.data.records[i].param_category==4){//分类4
                                            if(result.data.records[i].device_type_param_id==""){//未选中
                                                showCtrParamsBoxShowHtml4 +='<span><input name="paramszd" type="checkbox" value="'+result.data.records[i].id+'" /><label>'+result.data.records[i].param_name+'</label></span>';
                                            }else{//选中
                                                showCtrParamsBoxShowHtml4 +='<span><input name="paramszd" type="checkbox" value="'+result.data.records[i].id+'" checked="checked" /><label>'+result.data.records[i].param_name+'</label></span>';
                                            }
                                            $(".showCtrParamsBox4").html(showCtrParamsBoxShowHtml4);
                                        }else if(result.data.records[i].param_category==5){//分类5
                                            if(result.data.records[i].device_type_param_id==""){//未选中
                                                showCtrParamsBoxShowHtml5 +='<span><input name="paramszd" type="checkbox" value="'+result.data.records[i].id+'" /><label>'+result.data.records[i].param_name+'</label></span>';
                                            }else{//选中
                                                showCtrParamsBoxShowHtml5 +='<span><input name="paramszd" type="checkbox" value="'+result.data.records[i].id+'" checked="checked" /><label>'+result.data.records[i].param_name+'</label></span>';
                                            }
                                            $(".showCtrParamsBox5").html(showCtrParamsBoxShowHtml5);
                                        }else if(result.data.records[i].param_category==6){//分类6
                                            if(result.data.records[i].device_type_param_id==""){//未选中
                                                showCtrParamsBoxShowHtml6 +='<span><input name="paramszd" type="checkbox" value="'+result.data.records[i].id+'" /><label>'+result.data.records[i].param_name+'</label></span>';
                                            }else{//选中
                                                showCtrParamsBoxShowHtml6 +='<span><input name="paramszd" type="checkbox" value="'+result.data.records[i].id+'" checked="checked" /><label>'+result.data.records[i].param_name+'</label></span>';
                                            }
                                            $(".showCtrParamsBox6").html(showCtrParamsBoxShowHtml6);
                                        }else if(result.data.records[i].param_category==7){//分类7
                                            if(result.data.records[i].device_type_param_id==""){//未选中
                                                showCtrParamsBoxShowHtml7 +='<span><input name="paramszd" type="checkbox" value="'+result.data.records[i].id+'" /><label>'+result.data.records[i].param_name+'</label></span>';
                                            }else{//选中
                                                showCtrParamsBoxShowHtml7 +='<span><input name="paramszd" type="checkbox" value="'+result.data.records[i].id+'" checked="checked" /><label>'+result.data.records[i].param_name+'</label></span>';
                                            }
                                            $(".showCtrParamsBox7").html(showCtrParamsBoxShowHtml7);
                                        }else{//分类8
                                            if(result.data.records[i].device_type_param_id==""){//未选中
                                                showCtrParamsBoxShowHtml8 +='<span><input name="paramszd" type="checkbox" value="'+result.data.records[i].id+'" /><label>'+result.data.records[i].param_name+'</label></span>';
                                            }else{//选中
                                                showCtrParamsBoxShowHtml8 +='<span><input name="paramszd" type="checkbox" value="'+result.data.records[i].id+'" checked="checked" /><label>'+result.data.records[i].param_name+'</label></span>';
                                            }
                                            $(".showCtrParamsBox8").html(showCtrParamsBoxShowHtml8);
                                        }
                                    }
                                } 
                            });                
                        }
                        checkEveryDevicesParams(deFault_typeID);

                        //========默认选中第一个控制器，并显示第一个控制器参数END========
                        
                        
                        //========若选中对应的控制器，则右边显示对应控制器的参数与SATRT========
                        $(".baseInfosearchlistEach").on("click",function(){
                            $(this).find("i").addClass("baseInfo-checked").parent().siblings().find("i").removeClass("baseInfo-checked");
                            var curent_typeID = $(this).attr("data-id");
                            checkEveryDevicesParams(curent_typeID);

                        })
                        //========若选中对应的控制器，则右边显示对应控制器的参数与END========

            }
            


        } 
    });    
}
getAllDeviCtrTypes();


//个人中心下拉
$("#showmyAccountMenu").toggle(
    function(){$("#toggleshowmenu").show();},
    function(){$("#toggleshowmenu").hide();}
);

//$.flytip("zhowdom");$.vloading.show();$.vloading.remove();
getOnelayerMenu();
getTwolayerMenu(601,602);
$(".mianshowbpt-r a").eq(0).addClass("cur");
getThreelayerMenu(602);

//新增控制器类型弹层
$(".newAddDeviCtr").on("click",function(){
    $(".newAddCTRtypes,.masking").fadeIn();
})
$(".newEditDeviCtr").on("click",function(){
    $("input[name='deviceCtrIDb']").val($(".baseInfo-checked").parent(".baseInfosearchlistEach").attr("data-id"));
    $("input[name='deviceCtrTypeb']").val($(".baseInfo-checked").siblings().html());
    $(".newAddCTRtypesb,.masking").fadeIn();
})
$(".closeLayerbox,.cancelEdit,.cancelEditb").on("click",function(){
   $(".newAddCTRtypes,.newAddCTRtypesb,.masking").fadeOut(); 
})
//新增控制器类型提交
$(".sureSave").on("click",function(){
    var deviceCtrID = $("input[name='deviceCtrID']").val();
    var deviceCtrType = $("input[name='deviceCtrType']").val();
    if(deviceCtrID==""){
        $.flytip("请输入控制器ID");
        return false;
    }else if(deviceCtrType==""){
        $.flytip("请输入控制器类型");
        return false;
    }else{
            $.ajax({ 
                type:"post", 
                url:urls.newAddDeviCtrType, 
                data: {token:token,id:deviceCtrID,typeName:deviceCtrType}, 
                dataType: 'json', 
                async : true, 
                success:function(result){
                    $.flytip(result.msg);
                    getAllDeviCtrTypes();
                    $(".newAddCTRtypes,.newAddCTRtypesb,.masking").fadeOut();
                } 
            });
    }

})

//控制类型编辑
$(".sureSaveb").on("click",function(){
    var id = $(".baseInfo-checked").parent().attr("data-id");
    var typeName = $("input[name='deviceCtrTypeb']").val();
    $.ajax({ 
        type:"post", 
        url:urls.editDeviCtrType, 
        data: {token:token,id:id,typeName:typeName}, 
        dataType: 'json', 
        async : true, 
        success:function(result){
            $.flytip(result);
            getAllDeviCtrTypes();
            $(".newAddCTRtypes,.newAddCTRtypesb,.masking").fadeOut();
        } 
    });
    
})

//=====切换右边tab显示SATRT=====
$(".baseInfob-rShowDetNav a").on("click",function(){
    var eqIndexs = $(this).index();
    $(this).addClass("cur").siblings().removeClass("cur");
    $(".baseInfob-rShowDetNavTabshow").eq(eqIndexs).fadeIn().siblings(".baseInfob-rShowDetNavTabshow").fadeOut();
})
//=====切换右边tab显示END=====

//===参数修改保存START===
$(".baseInfoSaveBtn").on("click",function(){
    var device_type_id = $(".baseInfo-checked").parent().attr("data-id");
    var Params_Arr=new Array(); 
    var allChoosedCheckboxNums=document.getElementsByName('paramszd'); 
    for(var i=0;i<allChoosedCheckboxNums.length;i++){ 
        if(allChoosedCheckboxNums[i].checked==true){ 
                Params_Arr.push(allChoosedCheckboxNums[i].value); 
            } 
    } 

    $.ajax({ 
        type:"post", 
        url:urls.saveDeviceTypeParam, 
        data: {token:token,device_type_id:device_type_id,ids:Params_Arr.join(",")}, 
        dataType: 'json', 
        async : true, 
        success:function(result){
            $.flytip(result.msg);
        } 
    });



})
//===参数修改保存END===

//===搜索设备START===
$(".searchYouDevices").on("click",function(){
    getAllDeviCtrTypes();
})
//===搜索设备END===

//===全选START===
$("#ckAll").live("click", function() {
    $("input[name='paramszd']").prop("checked", this.checked);
});
  
$("input[name='paramszd']").live("click", function() {
    var $subs = $("input[name='paramszd']");
    $("#ckAll").prop("checked" , $subs.length == $subs.filter(":checked").length ? true :false);
});
//===全选END===

//req.js end
})